<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-header">
        <img class="logo mr10" src="../../assets/img/logo.svg" alt="" />
        <div class="login-title">练庆杨私人的后台管理系统</div>
      </div>
      <el-form :model="param" :rules="rules" ref="register" size="large">
        <!-- 表单信息 -->
        <!-- 用户名 -->
        <el-form-item prop="user_name">
          <el-input v-model="param.user_name" placeholder="用户名">
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item prop="user_email">
          <el-input v-model="param.user_email" placeholder="邮箱">
            <template #prepend>
              <el-icon>
                <Message />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item prop="user_phone">
          <el-input v-model="param.user_phone" placeholder="手机号">
            <template #prepend>
              <el-icon>
                <Phone />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="user_pwd">
          <el-input
            type="password"
            placeholder="密码"
            v-model="param.user_pwd"
            @keyup.enter="submitForm(register)"
          >
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 交互点击选项卡 -->
        <el-button
          class="login-btn"
          type="primary"
          size="large"
          @click="submitForm(register)"
          >注册</el-button
        >
        <p class="login-text">
          已有账号，<el-link type="primary" @click="$router.push('/login')"
            >立即登录</el-link
          >
        </p>
        <!-- 表单信息 -->
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { Register } from "@/types/user"; // 类型约束
import { registerAPI } from "../../api/index"; // 引入注册接口使用

const router = useRouter();
const param = reactive<Register>({
  user_name: "",
  user_email: "",
  user_phone: "",
  user_pwd: "",
});

const rules: FormRules = {
  user_name: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  user_email: [
    { required: true, message: "请输入邮箱", trigger: "blur" }, // 留空提示
    {
      // 邮箱验证提示
      pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
      message: "请输入正确的邮箱格式",
      trigger: "blur",
    },
  ],
  user_phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
  user_pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
const register = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
  // console.log(param);

  if (!formEl) return; // 输入框为空，终止代码！
  formEl.validate((valid: boolean) => {
    if (valid) {
      // ElMessage.success('注册成功，请登录');
      // router.push('/login');
      let { user_name, user_email, user_phone, user_pwd } = param;

      registerAPI({ user_name, user_email, user_phone, user_pwd })
        .then((content) => {
          //   console.log("成功！", content);
          let { code, msg } = content.content;

          if (code === 200) {
            ElMessage.success(msg);
            setTimeout(() => {
              router.push("/login");
            }, 1000);
          } else {
            ElMessage.success(msg);
            setTimeout(() => {
              router.push("/login");
            }, 1000);
          }
        })
        .catch((err) => {
          //   console.log("失败！", err);
          ElMessage.error("服务器异常！请检查接口参数配置是否正确！！！");
        });
    } else {
      return false;
    }
  });
};
</script>

<style scoped>
.login-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40px;
}

.logo {
  width: 35px;
}

.login-title {
  font-size: 22px;
  color: #333;
  font-weight: bold;
}

.login-container {
  width: 450px;
  border-radius: 5px;
  background: #fff;
  padding: 40px 50px 50px;
  box-sizing: border-box;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-text {
  display: flex;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
  color: #787878;
}
</style>
